import http from '@/utils/http'
import {createAction} from 'redux-actions'
import {CLEARSUGGESTION, SETSEARCHRESULT, SETSUGGESTIONLIST} from "@/store/actionTypes";


const setSuggestionList = createAction(SETSUGGESTIONLIST)
export const clearSuggestion = createAction(CLEARSUGGESTION)
const setSearchResult = createAction(SETSEARCHRESULT) // {type: 'SETSEARCHRESULT', payload: ''}

/**
 * 获取用户输入建议thunk函数
 * @param {String} value 用户输入的关键字
 */
export const getSuggestion = value => async dispatch => {
//  发送请求
  const res = await http.get('/suggestion', {
    params: {
      q: value
    }
  })

  // 当前获取到的数据写入redux
  console.log(res.data.options)
  dispatch(setSuggestionList(res.data.options))
}

/**
 * 获取搜索的结果
 */
export const getSearchResult = query => async dispatch => {
  const res = await http.get('/search', {
    params: {
      q: query,
      page: 1,
    },
  });


  dispatch(setSearchResult(res.data))
}

